package com.karaku.karaku_collector.common;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.test.ProviderTestCase2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.karaku.karaku_collector.R;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseActivity extends AppCompatActivity {

    protected View viewPageRoot;
    private TextView tvPageLeftBtn;
    private TextView tvPageRightBtn;
    private TextView tvTitle;
    private FrameLayout pageTitleBar;
    private FrameLayout flPageContent;
    private View viewWaiting;

    protected boolean isPaused;
    protected boolean isDestroyed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        KarakuActivityManager.addActivity(this);
        super.setContentView(R.layout.activity_base);

        viewPageRoot = findViewById(R.id.view_page_root);
        tvPageLeftBtn = (TextView) findViewById(R.id.tv_page_left_btn);
        tvPageRightBtn = (TextView) findViewById(R.id.tv_page_right_btn);
        tvTitle = (TextView) findViewById(R.id.tv_page_title);
        pageTitleBar = (FrameLayout) findViewById(R.id.page_title_bar);
        flPageContent = (FrameLayout) findViewById(R.id.fl_page_content);
        viewWaiting = findViewById(R.id.view_page_waiting);

        tvPageLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPageLeftBtnClick();
            }
        });
        tvPageRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPageRightBtnClick();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        KarakuActivityManager.onResume(this);
        isPaused = false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        KarakuActivityManager.onPause(this);
        isPaused = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KarakuActivityManager.removeActivity(this);
        isDestroyed = true;
    }

    @Override
    public void setTitle(CharSequence title) {
        tvTitle.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        tvTitle.setText(titleId);
    }

    public void setTitleImage(int resId) {
        Drawable d = ContextCompat.getDrawable(this, resId);
        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
        tvTitle.setCompoundDrawables(null, null, d, null);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        LayoutInflater.from(this).inflate(layoutResID, flPageContent, true);
    }

    @Override
    public void setContentView(View view) {
        FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        flPageContent.addView(view, p);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        flPageContent.addView(view, params);
    }

    @Override
    public void onBackPressed() {
        onBack();
    }

    protected void showWaiting() {
        viewWaiting.setVisibility(View.VISIBLE);
    }

    protected void hideWaiting() {
        viewWaiting.setVisibility(View.GONE);
    }

    protected void hideTitleBar() {
        pageTitleBar.setVisibility(View.GONE);
    }

    protected void showTitleBar() {
        pageTitleBar.setVisibility(View.VISIBLE);
    }

    protected void hideStatusBar() {
        if (Build.VERSION.SDK_INT >= 14) {
            viewPageRoot.setFitsSystemWindows(false);
        }
    }

    protected void showStatusBar() {
        if (Build.VERSION.SDK_INT >= 14) {
            viewPageRoot.setFitsSystemWindows(true);
        }
    }

    protected void setPageBackgroundRes(int resId) {
        viewPageRoot.setBackgroundResource(resId);
    }

    protected void setPageContentBackgroundRes(int resId) {
        flPageContent.setBackgroundResource(resId);
    }

    protected void showPageLeftBtn(String text) {
        tvPageLeftBtn.setText(text);
        tvPageLeftBtn.setVisibility(View.VISIBLE);
        tvPageLeftBtn.setCompoundDrawables(null, null, null, null);
    }

    protected void showPageLeftBtnIcon(@DrawableRes int drawableId) {
        Drawable d = ContextCompat.getDrawable(this, drawableId);
        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
        tvPageLeftBtn.setCompoundDrawables(d, null, null, null);
        tvPageLeftBtn.setVisibility(View.VISIBLE);
        tvPageLeftBtn.setText("");
    }

    protected void hidePageLeftBtn() {
        tvPageLeftBtn.setVisibility(View.GONE);
    }

    protected void showPageRightBtn(String text) {
        tvPageRightBtn.setText(text);
        tvPageRightBtn.setVisibility(View.VISIBLE);
    }

    protected void showPageRightBtn(int img) {
        Drawable d = ContextCompat.getDrawable(this, img);
        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
        tvPageRightBtn.setCompoundDrawables(null, null, d, null);
        tvPageRightBtn.setVisibility(View.VISIBLE);
    }

    protected void hidePageRightBtn() {
        tvPageRightBtn.setVisibility(View.GONE);
    }

    protected void onPageLeftBtnClick() {
        onBack();
    }

    protected void onPageRightBtnClick() {
    }

    protected void onBack() {
        finish();
    }

    protected void updatePageAnim(boolean isGotoMore) {
        if (isGotoMore) {
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        } else {
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

}
